package tau.dimes.visualization.applet.topology;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tau.dimes.visualization.applet.Visualization;
import tau.dimes.visualization.applet.data.Edge;
import tau.dimes.visualization.applet.data.Hop;
import tau.dimes.visualization.applet.data.Trace;

/**
 * Implements the topology visualization.
 */
public class Topology extends Visualization
{
	private final List<TopologyTrace> traces;
	private final Map<String, TopologyNode> nodes;
	
	/**
	 * Constructs a new Topology.
	 */
	public Topology()
	{
		nodes = new HashMap<String, TopologyNode>();
		traces = new ArrayList<TopologyTrace>();
	}

	@Override
	public void init()
	{
		for (Hop hop : traceDataSet.getHops().values())
			nodes.put(hop.getId(), new TopologyNode(applet, hop.getId(), applet.mapHAngle(hop
				.getLongitude()), applet.mapVAngle(hop.getLatitude())));
		
		for (Trace trace : traceDataSet.getTraces())
		{
			TopologyTrace topologyTrace = new TopologyTrace();
			for (Edge edge : trace)
				topologyTrace.addArc(new TopologyArc(applet, edge, nodes));
			traces.add(topologyTrace);
		}
	}

	@Override
	public void draw(int timeSlot, float alpha)
	{
		applet.smooth();
		
		applet.strokeWeight(1);
		applet.stroke(255, 127, 0);
		applet.noFill();

		for (TopologyTrace trace : traces)
			trace.draw(timeSlot, alpha);

		applet.stroke(0, 0, 0);
		applet.fill(0, 255, 255);

		for (TopologyNode node : nodes.values())
			node.draw();
		
		applet.noSmooth();
	}

	@Override
	public int getTimeSlots()
	{
		return traceDataSet.getMaxEdges();
	}
}
